package com.atguigu.factory.bSimpleFactory.pizzaStore.order;

import com.atguigu.factory.bSimpleFactory.pizzaStore.pizza.Pizza;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @program: design-patterns
 * @author: yangshen
 * @description: 订购披萨
 * @create: 2022-01-18 21:44
 **/
public class OrderPizza {

    /**
     * 定义一个简单工厂
     */
    SimpleFactory simpleFactory;
    /**
     * 定义一个披萨
     */
    Pizza pizza;

    /**
     * 构造器    （采用聚合的方式将SimpleFactory传递进来）
     * @param simpleFactory 简单工厂类
     */
    public OrderPizza(SimpleFactory simpleFactory) {
        setFactory(simpleFactory);
    }

    public void setFactory(SimpleFactory simpleFactory) {
        //用户输入披萨类型
        String orderType = "";
        this.simpleFactory = simpleFactory;
        do {
            orderType = getOrderType();
            pizza = this.simpleFactory.createPizza(orderType);
            //输出披萨信息
            if (null != pizza){
                //订购成功，并输出
                pizza.prepare();
                pizza.bake();
                pizza.cut();
                pizza.box();
            }else {
                System.out.println("订购披萨失败");
                break;
            }
        }while (true);
    }

    /**
     * 写一个方法，可以获取客户希望订购的披萨种类
     * @return 披萨种类
     */
    public String getOrderType(){
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("--------------------------");
        System.out.println("input pizza 种类：");

        try {
            String s = reader.readLine();
            return s;
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }

    }
}
